package com.hy.admin.service.impl;

import com.hy.admin.dao.AccountDao;
import com.hy.admin.dao.MemberDao;
import com.hy.admin.entity.AccountEntity;
import com.hy.admin.entity.MemberEntity;
import com.hy.admin.enums.EnumAuditStatus;
import com.hy.admin.enums.EnumMemberType;
import com.hy.admin.helper.request.AccountRequest;
import com.hy.admin.service.AccountService;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * Created by shitou on 17/12/25.
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private MemberDao memberDao;
    @Autowired
    private AccountDao accountDao;

    @Override
    public ServicesResult<MemberEntity> init(String deviceIdentify) {
        ServicesResult<MemberEntity> servicesResult = new ServicesResult<>();
        MemberEntity memberEntity = memberDao.selectDeviceIdentify(deviceIdentify);
        if(memberEntity == null){
            memberEntity = new MemberEntity();
            memberEntity.setDeviceIdentify(deviceIdentify);
            memberEntity.setStatus(0);
            memberEntity.setCreateId(0);
            memberEntity.setUpdateId(0);
            memberDao.initNew(memberEntity);
        }
        servicesResult.setResult(memberEntity);
        return servicesResult;
    }

    @Override
    @Transactional
    public ServicesResult<MemberEntity> Register(AccountRequest accountRequest) {
        ServicesResult<MemberEntity> servicesResult = new ServicesResult<>();
        List<MemberEntity> memberEntityList = memberDao.selectByMobile(accountRequest.getMobile());
        if (!memberEntityList.isEmpty()) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("手机号已注册");
            return servicesResult;
        }
        MemberEntity memberEntity = new MemberEntity();
//        if(accountRequest.getMemberId() != null && accountRequest.getMemberId() > 0){
        //原注册逻辑
        memberEntity.setStatus(0);
        memberEntity.setMobile(accountRequest.getMobile());
        memberEntity.setCreateId(0);
        memberEntity.setUpdateId(0);
        memberDao.init(memberEntity);
//        }else {
//            memberEntity.setMobile(accountRequest.getMobile());
//            memberEntity.setId(accountRequest.getMemberId());
//            memberDao.bindMobile(memberEntity);
//        }

        if (memberEntity.getAccountId() == null
                || memberEntity.getAccountId() <= 0) {
            AccountEntity accountEntity = new AccountEntity();
            accountEntity.setEnabled(true);
            accountEntity.setUserName(accountRequest.getMobile());
            accountEntity.setPassword(accountRequest.getPassword());
            accountDao.insert(accountEntity);
            memberEntity.setAccountId(accountEntity.getId());
            memberDao.updateAccountId(memberEntity);
        }
        servicesResult.setResult(memberEntity);
        return servicesResult;
    }

    @Override
    public ServicesResult<MemberEntity> login(AccountRequest accountRequest) {
        ServicesResult<MemberEntity> servicesResult = new ServicesResult<>();
        AccountEntity accountEntity = accountDao.selectByLoginName(accountRequest.getMobile());
        if (accountEntity == null) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("手机号未注册");
            return servicesResult;
        }
        if (!accountRequest.getPassword().equals(accountEntity.getPassword())) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("密码错误");
            return servicesResult;
        }
        List<MemberEntity> memberEntityList = memberDao.selectByMobile(accountRequest.getMobile());
        if (memberEntityList.isEmpty()) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("手机号未注册");
            return servicesResult;
        }
        //如果用户为教师，判断教师审核状态，未审核或审核不通过，禁止登录
        MemberEntity member = memberEntityList.get(0);
        if(member != null && member.getMemberType() != null && member.getMemberType() == EnumMemberType.Teacher.getId()
                && member.getAuditStatus() != EnumAuditStatus.PASS.getId()){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("未通过审核");
            return servicesResult;
        }
        servicesResult.setResult(memberEntityList.get(0));
        return servicesResult;
    }

    @Override
    public ServicesResult<MemberEntity> getMember(Long memberId) {
        ServicesResult<MemberEntity> servicesResult = new ServicesResult<>();
        if (memberId== null || memberId <= 0) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        MemberEntity member = memberDao.selectById(memberId);
        servicesResult.setResult(member);
        return servicesResult;
    }

    @Override
    public ServicesResult<Boolean> updatePwd(String mobile, String password) {
        ServicesResult<Boolean> servicesResult = new ServicesResult<>();
        if (StringUtils.isEmpty(mobile)) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        if (StringUtils.isEmpty(password)) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        List<MemberEntity> memberEntityList = memberDao.selectByMobile(mobile);
        if(memberEntityList.isEmpty()){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("用户不存在");
            return servicesResult;
        }

        int count = accountDao.updatePassword(memberEntityList.get(0).getAccountId(), password);
        servicesResult.setResult(count>0);
        return servicesResult;
    }

    @Override
    public ServicesResult<Boolean> resetPwd(String oldPassword, String mobile, String newPassword) {
        ServicesResult<Boolean> servicesResult = new ServicesResult<>();
        if (StringUtils.isEmpty(mobile)) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        if (StringUtils.isEmpty(oldPassword)) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        if (StringUtils.isEmpty(newPassword)) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("请求报文有误");
            return servicesResult;
        }
        List<MemberEntity> memberEntityList = memberDao.selectByMobile(mobile);
        if(memberEntityList.isEmpty()){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("用户不存在");
            return servicesResult;
        }
        AccountEntity account = accountDao.selectByLoginName(mobile);
        if( account == null ){
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("账户不存在");
            return servicesResult;
        }
        if(!oldPassword.equals(account.getPassword())) {
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("原密码不正确");
            return servicesResult;
        }
        int count = accountDao.updatePassword(memberEntityList.get(0).getAccountId(), newPassword);
        servicesResult.setResult(count>0);
        return servicesResult;
    }
}
